﻿Imports BUS
Imports DTO
Public Class frmSanh
    Dim i As Integer = 0
    Private Sub frmSanh_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        load_sanh()
        load_loaisanh()
        khoa_data()
    End Sub
    Public Sub load_sanh()
        Dim db As New DataTable()
        Dim ds As New SANHBUS
        db = ds.load_sanh()
        luoi.DataSource = db
        luoi.[ReadOnly] = True
        ' luoi.Columns["PASS"].Visible = false;
        Dim i As Integer
        For i = 0 To db.Rows.Count - 1
            luoi.Rows(i).Cells(0).Value = (i + 1)

        Next i

        

        th_ghichu.DataBindings.Clear()
        th_ghichu.DataBindings.Add("Text", db, "GHICHU")

        th_soluongbantoida.DataBindings.Clear()
        th_soluongbantoida.DataBindings.Add("Text", db, "SOLUONGBANTOIDA")

        th_tensanh.DataBindings.Clear()
        th_tensanh.DataBindings.Add("Text", db, "TENSANH")



    End Sub
    Dim masanh_ As Integer
    Public Sub load_loaisanh()
        Dim db As New DataTable()
        Dim ds As New LOAISANHBUS()
        db = ds.load_loaisanh()
        cbo_loaisanh.DataSource = db
        cbo_loaisanh.ValueMember = "MALOAISANH"
        cbo_loaisanh.DisplayMember = "TENLOAISANH"
    End Sub

    Private Sub luoi_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles luoi.CellContentClick
        cbo_loaisanh.SelectedValue = luoi.CurrentRow.Cells("MALOAISANH").Value.ToString
        cbo_loaisanh.DisplayMember = cbo_loaisanh.SelectedValue
        masanh_ = luoi.CurrentRow.Cells("MASANH").Value.ToString
    End Sub
    Public Sub mo_data_them()
        xl.Enabled = True
        th_ghichu.Text = ""
        th_tensanh.Text = ""
        th_soluongbantoida.Text = ""
        
        XL1.Visible = False
        xl2.Visible = True
    End Sub
    Public Sub mo_data_capnhat()
        xl.Enabled = True
        xl1.Visible = False
        xl2.Visible = True
    End Sub
    Public Sub khoa_data()

        xl.Enabled = False
        xl1.Visible = True
        xl2.Visible = False
        load_sanh()
    End Sub

    Private Sub but_them_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_them.Click
        i = 1
        mo_data_them()
    End Sub

    Private Sub but_sua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_sua.Click
        i = 0
        mo_data_capnhat()
    End Sub

    Private Sub but_thoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_thoat.Click
        Me.Close()
    End Sub

    Private Sub but_xoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_xoa.Click
        Dim dto As New SANHDTO()
        Dim bus As New SANHBUS()
        dto = getinfo()

        Dim dlr As New DialogResult()
        dlr = MessageBox.Show("Bạn có chắn là xóa mẫu tin này không?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
        If dlr = DialogResult.OK Then
            If bus.xoa_sanh(dto) = True Then
                MessageBox.Show("Xoa thanh cong")
                load_sanh()
            Else
                MessageBox.Show("Du lieu chua xoa duoc, vui long kiem tra lai")

            End If
        End If
    End Sub
    Public Function getinfo() As SANHDTO
        Dim test As New kiemtra()
        Dim dto As New SANHDTO()
        ' gan dta
        dto.MASANH = masanh_

        If test.ktra_rong(th_ghichu.Text) = True Then
            dto.GHICHU = th_ghichu.Text
        Else
            MessageBox.Show("Chưa nhập ghi chú loại sảnh")
        End If

        If test.ktra_rong(th_soluongbantoida.Text) = True Then
            dto.SOLUONGBANTOIDA = Single.Parse(th_soluongbantoida.Text.ToString())
        Else
            MessageBox.Show("Chưa nhập số lượng bàn tối đa")
        End If

        If test.ktra_rong(th_tensanh.Text) = True Then
            dto.TENSANH = th_tensanh.Text
        Else
            MessageBox.Show("Chưa nhậP tên sảnh")
        End If
        dto.MALOAISANH = cbo_loaisanh.SelectedValue()

        Return dto
    End Function

    Private Sub but_luu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_luu.Click
        Dim dto As New SANHDTO()
        Dim bus As New SANHBUS()
        dto = getinfo()
        If i = 1 Then
            If bus.them_sanh(dto) = True Then
                MessageBox.Show("Them thanh cong")
                load_sanh()
                khoa_data()
            Else
                MessageBox.Show("Chua Them duoc")
            End If
        Else
            If bus.capnhat_sanh(dto) = True Then
                MessageBox.Show("Cap nhat thanh cong")
                load_sanh()
                khoa_data()
            Else
                MessageBox.Show("Chua cap nhat duoc")
            End If
        End If

    End Sub

    Private Sub but_kluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_kluu.Click
        khoa_data()
        load_sanh()
    End Sub

    Private Sub th_soluongbantoida_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles th_soluongbantoida.TextChanged

    End Sub

    Private Sub th_soluongbantoida_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles th_soluongbantoida.KeyPress
        If Not [Char].IsDigit(e.KeyChar) AndAlso Not [Char].IsControl(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub
End Class